<?php

namespace App\Http\Controllers;

use App\Models\Project;
use App\Models\Inspection;
use Illuminate\Http\Request;
use App\Models\Inspection\Node;
use App\Models\Inspection\Report;
use Illuminate\Support\Facades\Auth;
use App\Repositories\InspectionRepository;
use App\Http\Requests\Logic\Inspection\StoreResource;

/**
 * 巡检业务逻辑控制器
 *
 * Date: 25/04/2018
 * @author George
 * @package App\Http\Controllers
 */
class InspectionController extends Controller
{
	protected $inspectionRepository;

	/**
	 * InspectionController constructor.
	 * @param InspectionRepository $inspectionRepository
	 */
	public function __construct(InspectionRepository $inspectionRepository)
	{
		$this->inspectionRepository = $inspectionRepository;
	}

	/**
	 * Date: 01/05/2018
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
	 */
    public function index(Request $request)
    {
    	$user = Auth::user();
    	$start = $request->get('start');
    	$end = $request->get('end');
    	$query = Project::query();
    	return $query->with('lines.circles.nodes')->where('company_id', $user->company_id)->get();
    }

	/**
	 * 创建巡检记录
	 *
	 * Date: 27/04/2018
	 * @author George
	 * @param StoreResource $request
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \App\Exceptions\GeneralException
	 */
    public function store(StoreResource $request)
    {
    	$attributes = $this->extractInputFromRules($request, $request->rules());
    	array_pull($attributes, 'images');
    	$node = Node::with('circle.line')->where('id', $attributes['node_id'])->first();
    	$user = Auth::user();

    	if (! $node) {
    		return failed('巡检节点不存在');
		}

		if (! $node->circle) {
    		return failed('巡检节点未关联到圈');
		}

		if (! $node->circle->line) {
    		return failed('巡检节点未关联到线路');
		}

		$inspection = $this->inspectionRepository->store($user, $attributes, $request->file('images'), $node);

    	return stored($inspection);
	}

	/**
	 * 获取巡检节点详情
	 *
	 * Date: 25/04/2018
	 * @author George
	 * @param Inspection $inspection
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function show(Inspection $inspection)
    {
    	$inspection->images = $inspection->images()->get();
    	return success($inspection);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Inspection  $inspection
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Inspection $inspection)
    {
        // todo
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Inspection  $inspection
     * @return \Illuminate\Http\Response
     */
    public function destroy(Inspection $inspection)
    {
        // todo
    }

	/**
	 * 获取巡检数据报表
	 *
	 * Date: 01/05/2018
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function report(Request $request)
	{
		$start = $request->get('start');
		$end = $request->get('end');
		$project_id = $request->get('project_id');
		$paginate = $request->get('paginate', 15);
		$query = Report::query();
		if ($project_id) {
			$query->where('project_id', $project_id);
		}

		if ($start && $end) {
			$query->whereBetween('date', [$start, $end]);
		}
		return success($query->paginate($paginate));
    }
}
